package org.jeecg.modules.monitoring.j_monitoring.gatewayData;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.xkcoding.http.util.StringUtil;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.config.sign.util.NoSslHttpUtils;
import org.jeecg.config.vo.HttpResult;
import org.jeecg.modules.monitoring.j_monitoring.entity.RealStorage;
import org.jeecg.modules.monitoring.j_monitoring.entity.RealStorageItem;
import org.jeecg.modules.monitoring.j_monitoring.entity.RealStorageLun;
import org.jeecg.modules.monitoring.j_monitoring.entity.fdnEntity.*;
import org.jeecg.modules.monitoring.j_monitoring.mapper.RealStorageItemMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.RealStorageLunMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.RealStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 储存资源获取数据
 */
@Component
public class StorageResources {
    @Autowired
    private Environment env;
    private String deviceMonitors = "/ng/integration/deviceMonitors";

    private String deviceType = "/ng/integration/deviceMonitorsSummary";

    private String monitorTypePath = "classpath:jsonFile/gateway/sugonMo.json";

    private String monitorDataPath = "classpath:jsonFile/gateway/huaweidata.json";

    private String childMos = "/ng/mo/childMOs";


    @Resource
    private RealStorageItemMapper storageItemMapper;

    @Resource
    private RealStorageMapper storageMapper;

    @Resource
    private RealStorageLunMapper lunMapper;


    public StorageResultDataNew getData(String fdn) {

        StorageResultDataNew resultData = new StorageResultDataNew();
        String brand = getType(fdn);

        String json = this.getDeviceMonitors(fdn);
        if (StringUtil.isEmpty(json)) {
            return resultData;
        }

        switch (brand) {
            case "huawei":
                resultData = this.getHuaweiData(json);
                this.getRealControllerData(fdn, brand, resultData);
                break;
            case "sugon":
                resultData = this.getSugonData(json, fdn);
                this.getRealControllerData(fdn, brand, resultData);
                break;
            case "dell":
                resultData = this.getDellData(json);
                break;
            case "netApp":
                resultData = this.getNetAppData(json);
                break;
            default:
                return resultData;
        }

        return resultData;
    }

    private int getNameSortByBrand(String name, String brand) {
        if ("sugon".equals(brand) && name.contains("#")) {
            String[] names = name.split("#");
            return new Integer(names[1]);
        }


        if ("huawei".equals(brand) && name.contains(".")) {
            String[] names = name.split("\\.");
            return new Integer(names[1]);
        }

        return 0;
    }

    private int getKeywordSortByBrand(String keyword) {
        if (keyword.contains("DAE")) {
            String[] keywords = keyword.split("DAE");
            return new Integer(keywords[1]);
        }


        if (keyword.contains(".")) {
            String[] keywords = keyword.split("\\.");
            return new Integer(keywords[keywords.length - 1]);
        }

        return 0;
    }


    /**
     * 获取下级请求
     *
     * @param fdn
     * @return
     */
    public List<VirtuChildResult> getChildMOs(String fdn) {

        String url = env.getProperty("gatewaySystem.url");

        String header = env.getProperty("gatewaySystem.header");

        String queryUrl = url + childMos + "?fdn=" + fdn;

        String json = "";
        if (!"prod".equals(env.getProperty("spring.profiles.active"))) {
            return new ArrayList<>();

        }
        HttpResult res = NoSslHttpUtils.noSslGet(queryUrl, header);
        json = res.getBody();


        try {
            List<VirtuChildResult> result = JSONArray.parseArray(json, VirtuChildResult.class);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }

    }

    public String getDeviceMonitors(String fdn) {
        try {

            String url = env.getProperty("gatewaySystem.url");
            String header = env.getProperty("gatewaySystem.header");
            String queryUrl = url + deviceMonitors + "?device_fdns=" + fdn;
            String json = "";
            if (!"prod".equals(env.getProperty("spring.profiles.active"))) {
                File jsonFile = ResourceUtils.getFile(monitorDataPath);
                json = CommonUtils.getStr(jsonFile);

            } else {
                HttpResult res = NoSslHttpUtils.noSslGet(queryUrl, header);
                json = res.getBody();
            }
            return json;
        } catch (Exception e) {
            System.out.println(e);
        }
        return "";
    }

    public StorageResultDataNew getHuaweiData(String json) {
        StorageResultDataNew resultData = new StorageResultDataNew();
        try {

            List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
            DevicesMonitors monitors = monitorsList.get(0);
            BigDecimal used = new BigDecimal(0);
            Integer normalFan = 0;
            Integer warningFan = 0;

            Integer normalPower = 0;
            Integer warningPower = 0;

            List<Lun> lunList = new ArrayList<>();
            for (DevicesCounters counters : monitors.getMonitors()) {

                //lun
                if ("HuaweiStorageLUN".equals(counters.getType())) {
                    Lun lun = new Lun();
                    //磁盘监控
                    for (DevicesData devicesData : counters.getCounters()) {

                        if ("HealthStatus".equals(devicesData.getName())) {
                            lun.setStatus("1".equals(devicesData.getValue()) ? "正常" : "异常");
                        }

                        if ("TotalCapacity".equals(devicesData.getName())) {
                            used = used.add(this.unitConversion(devicesData.getValue(), devicesData.getUnit()));
                            lun.setTotal(this.unitConversion(devicesData.getValue(), devicesData.getUnit()).toPlainString());
                        }

                    }

                    lun.setName(counters.getName());
                    lunList.add(lun);
                }

                if ("HuaweiStorageFan".equals(counters.getType()) && counters.getCounters().size() > 0) {

                    for (DevicesData devicesData : counters.getCounters()) {
                        if ("HealthStatus".equals(devicesData.getName())) {

                            if ("1".equals(devicesData.getValue())) {
                                normalFan++;
                            } else {
                                warningFan++;
                            }

                        }
                    }

                }

                if ("HuaweiStoragePower".equals(counters.getType()) && counters.getCounters().size() > 0) {
                    //响应时间
                    for (DevicesData devicesData : counters.getCounters()) {
                        if ("HealthStatus".equals(devicesData.getName())) {
                            if ("1".equals(devicesData.getValue())) {
                                normalPower++;
                            } else {
                                warningPower++;
                            }
                        }
                    }

                }

            }


            resultData.setUsedSize(used);
            resultData.setNormalFan(normalFan);
            resultData.setWarningFan(warningFan);
            resultData.setNormalPower(normalPower);
            resultData.setWarningPower(warningPower);

            resultData.setLunList(lunList);


        } catch (Exception e) {
            System.out.println(111);
            e.printStackTrace();
        }
        return resultData;
    }

    public StorageResultDataNew getSugonData(String json, String fdn) {
        StorageResultDataNew resultData = new StorageResultDataNew();
        try {

            List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
            DevicesMonitors monitors = monitorsList.get(0);
            for (DevicesCounters counters : monitors.getMonitors()) {

                //系统运行时间
                if ("SystemUptime".equals(counters.getType())) {

                    for (DevicesData devicesData : counters.getCounters()) {

                        if ("Uptime".equals(devicesData.getName())) {
                            resultData.setRunTime(devicesData.getValue().replaceAll("Uptime ", ""));
                        }

                    }

                }

            }

            BigDecimal used = new BigDecimal(0);
            List<RealStorageLun> lunList = lunMapper.selectByFdn(fdn);

            List<Lun> luns = new ArrayList<>();

            for (RealStorageLun storageLun : lunList) {
                Lun lun = new Lun();
                lun.setName(storageLun.getName());
                lun.setStatus(storageLun.getStatus());
                if (storageLun.getUsed() != null) {
                    used = used.add(storageLun.getUsed());
                    lun.setTotal(storageLun.getUsed().toPlainString());
                } else {
                    lun.setTotal("0");
                }
                luns.add(lun);
            }

            resultData.setLunList(luns);
            resultData.setUsedSize(used);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultData;
    }

    public void getRealControllerData(String fdn, String brand, StorageResultDataNew resultData) {

        RealStorage realStorage = storageMapper.selectByFdn(fdn);
        System.out.println(realStorage);

        int normalController = 0;
        int warningController = 0;

        int normalDisk = 0;
        int warningDisk = 0;


        if (realStorage == null) {
            return;
        }
        if (realStorage.getCpuProportion() != null) {
            resultData.setCpu(realStorage.getCpuProportion().stripTrailingZeros().toPlainString());
        }
        resultData.setPingTime(realStorage.getPingTime() + "ms");
        if (realStorage.getIops() != null) {
            resultData.setIOPS(realStorage.getIops().stripTrailingZeros().toPlainString());
        }

        if (realStorage.getTotalCapacity() != null) {
            resultData.setTotalCapacity(realStorage.getTotalCapacity().stripTrailingZeros().toPlainString() + "TB");
        }
        List<RealStorageItem> storageItemList = storageItemMapper.selectByFdn(fdn);

        if (storageItemList.size() == 0) {
            return;
        }

        Map<String, StorageCon> controllerMap = new HashMap<>();
        Map<String, StorageCon> diskMap = new HashMap<>();


        List<RealStorageItem> fanList = new ArrayList<>();
        List<RealStorageItem> powerList = new ArrayList<>();

        for (RealStorageItem storage : storageItemList) {

            if ("fan".equals(storage.getType())) {
                fanList.add(storage);
            }

            if ("power".equals(storage.getType())) {
                powerList.add(storage);
            }

            if ("controller".equals(storage.getType())) {

                if ("1".equals(storage.getStatus())) {
                    normalController++;
                } else {
                    warningController++;
                }

                if (controllerMap.get(storage.getKeyword()) == null) {

                    StorageCon con = new StorageCon();
                    con.setWarning(!"1".equals(storage.getStatus()));
                    List<RealStorageItem> itemList = con.getList();
                    itemList.add(storage);
                    con.setList(itemList);
                    con.setKeyword(storage.getKeyword());
                    controllerMap.put(storage.getKeyword(), con);
                } else {
                    StorageCon con = controllerMap.get(storage.getKeyword());
                    if (!con.getWarning()) {
                        con.setWarning(!"1".equals(storage.getStatus()));
                    }
                    con.getList().add(storage);
                    controllerMap.put(storage.getKeyword(), con);
                }

            }

            if ("disk".equals(storage.getType())) {

                if ("1".equals(storage.getStatus())) {
                    normalDisk++;
                } else {
                    warningDisk++;
                }

                if (diskMap.get(storage.getKeyword()) == null) {
                    StorageCon con = new StorageCon();
                    con.setWarning(!"1".equals(storage.getStatus()));
                    List<RealStorageItem> itemList = con.getList();
                    itemList.add(storage);
                    con.setList(itemList);
                    con.setKeyword(storage.getKeyword());
                    diskMap.put(storage.getKeyword(), con);
                } else {
                    StorageCon con = diskMap.get(storage.getKeyword());
                    if (!con.getWarning()) {
                        con.setWarning(!"1".equals(storage.getStatus()));
                    }
                    con.getList().add(storage);
                    diskMap.put(storage.getKeyword(), con);
                }

            }
        }

        List<StorageCon> storageConList = new ArrayList<>();
        for (Map.Entry<String, StorageCon> entry : controllerMap.entrySet()) {
            storageConList.add(entry.getValue());
        }

        List<StorageCon> diskList = new ArrayList<>();
        for (Map.Entry<String, StorageCon> entry : diskMap.entrySet()) {
            for (RealStorageItem i : entry.getValue().getList()) {
                i.setSort(getNameSortByBrand(i.getName(), i.getBrand()));
            }
            entry.getValue().getList().sort((x, y) -> Integer.compare(x.getSort(), y.getSort()));
            entry.getValue().setSort(getKeywordSortByBrand(entry.getValue().getKeyword()));
            diskList.add(entry.getValue());
        }
        diskList.sort((x, y) -> Integer.compare(x.getSort(), y.getSort()));

        /**
         * 新需求:曙光与华为区分展示
         */
        if ("sugon".equals(brand)) {
            List<RealSugonItem> sugonItemList = new ArrayList<>();

            for (StorageCon controller : storageConList) {

                for (StorageCon diskCon : diskList) {

                    if (controller.getKeyword().equals(diskCon.getKeyword())) {
                        RealSugonItem sugonItem = new RealSugonItem();
                        sugonItem.setIp(controller.getKeyword());
                        sugonItem.setControllerList(controller.getList());
                        sugonItem.setWarning(controller.getWarning() || diskCon.getWarning());
                        sugonItem.setDiskList(diskCon.getList());
                        sugonItemList.add(sugonItem);
                    }

                }

            }
            resultData.setSugonItemList(sugonItemList);
        }

        resultData.setPowerList(powerList);
        resultData.setFanList(fanList);
        resultData.setNormalDisk(normalDisk);
        resultData.setNormalController(normalController);
        resultData.setWarningDisk(warningDisk);
        resultData.setWarningController(warningController);
        resultData.setControllerList(storageConList);
        resultData.setDiskList(diskList);
        resultData.setBrand(brand);
    }


    public StorageResultDataNew getDellData(String json) {
        StorageResultDataNew resultData = new StorageResultDataNew();
        try {

            List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
            DevicesMonitors monitors = monitorsList.get(0);

            BigDecimal used = new BigDecimal(0);
            Integer normalFan = 0;
            Integer warningFan = 0;
            Integer normalPower = 0;
            Integer warningPower = 0;
            Integer normalDisk = 0;
            Integer warningDisk = 0;

            List<Lun> lunList = new ArrayList<>();
            for (DevicesCounters counters : monitors.getMonitors()) {

                //系统运行时间
                if ("SystemUptime".equals(counters.getType())) {

                    for (DevicesData devicesData : counters.getCounters()) {

                        if ("Uptime".equals(devicesData.getName())) {
                            resultData.setRunTime(devicesData.getValue().replaceAll("Uptime ", ""));
                        }

                    }

                }

                if ("PingMonitor".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                        for (DevicesData devicesData : counters.getCounters()) {
                            if ("Response Time".equals(devicesData.getName())) {
                                resultData.setPingTime(devicesData.getValue());
                            }
                        }
                    }
                }

                if ("EMCIsilonNodeCPU".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                        for (DevicesData devicesData : counters.getCounters()) {
                            if ("utilization".equals(devicesData.getName())) {
                                resultData.setCpu(devicesData.getValue()+devicesData.getUnit());
                            }
                        }
                    }
                }

                if ("DiskMonitor".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                        if (counters.getCounters().size()>0){
                            normalDisk++;
                        }else {
                            warningDisk++;
                        }
                    }
                }

                if ("EMCIsilonClusterIFSUtil".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                        for (DevicesData devicesData : counters.getCounters()) {
                            if ("Used".equals(devicesData.getName())) {
                                resultData.setUsedSize(this.unitConversion(devicesData.getValue(),devicesData.getUnit()));
                            }
                        }
                    }
                }

                if ("EMCIsilonClusterHealth".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                        for (DevicesData devicesData : counters.getCounters()) {

                            if ("OnlineNodes".equals(devicesData.getName()) && StringUtil.isNotEmpty(devicesData.getValue())) {
                                String[] onlineList = devicesData.getValue().split(",");
                                for (String n : onlineList){
                                    Lun lun = new Lun();
                                    lun.setName(n);
                                    lun.setStatus("正常");
                                    lunList.add(lun);
                                }
                            }

                            if ("OfflineNodes".equals(devicesData.getName()) && StringUtil.isNotEmpty(devicesData.getValue())) {

                                String[] onlineList = devicesData.getValue().split(",");
                                for (String n : onlineList){
                                    Lun lun = new Lun();
                                    lun.setName(n);
                                    lun.setStatus("异常");
                                    lunList.add(lun);
                                }
                            }

                        }
                    }
                }




            }

            resultData.setUsedSize(this.showUnit(used));
            resultData.setNormalPower(normalPower);
            resultData.setWarningPower(warningPower);
            resultData.setNormalFan(normalFan);
            resultData.setWarningFan(warningFan);
            resultData.setNormalDisk(normalDisk);
            resultData.setWarningDisk(warningDisk);
            resultData.setLunList(lunList);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultData;
    }


    public BigDecimal syncNetAppUsed(String fdn) {
        try {

            String json = this.getDeviceMonitors(fdn);
            List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
            DevicesMonitors monitors = monitorsList.get(0);

            BigDecimal used = new BigDecimal(0);

            for (DevicesCounters counters : monitors.getMonitors()) {


                if ("NetAppsDiskMonitor".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size()>0) {
                        Lun lun = new Lun();
                        for (DevicesData devicesData : counters.getCounters()) {
                            if ("Used Size".equals(devicesData.getName())) {
                                used = used.add(this.unitConversion(devicesData.getValue(), devicesData.getUnit()));
                            }
                        }
                    }
                }
            }
            return used;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BigDecimal(0);
    }

    public StorageResultDataNew getNetAppData(String json) {
        StorageResultDataNew resultData = new StorageResultDataNew();
        try {

            List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
            DevicesMonitors monitors = monitorsList.get(0);

            BigDecimal used = new BigDecimal(0);
            Integer normalFan = 0;
            Integer warningFan = 0;
            Integer normalPower = 0;
            Integer warningPower = 0;
            Integer normalDisk = 0;
            Integer warningDisk = 0;

            List<Lun> lunList = new ArrayList<>();
            for (DevicesCounters counters : monitors.getMonitors()) {

                //系统运行时间
                if ("SystemUptime".equals(counters.getType())) {

                    for (DevicesData devicesData : counters.getCounters()) {

                        if ("Uptime".equals(devicesData.getName())) {
                            resultData.setRunTime(devicesData.getValue().replaceAll("Uptime ", ""));
                        }

                    }

                }

                if ("PingMonitor".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                        for (DevicesData devicesData : counters.getCounters()) {
                            if ("Response Time".equals(devicesData.getName())) {
                                resultData.setPingTime(devicesData.getValue());
                            }
                        }
                    }
                }

                if ("NetAppsCPUMonitor".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                        for (DevicesData devicesData : counters.getCounters()) {
                            if ("CPU Utilization".equals(devicesData.getName())) {
                                resultData.setCpu(devicesData.getValue()+devicesData.getUnit());
                            }
                        }
                    }
                }

                if ("NetAppsDiskMonitor".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size()>0) {
                            Lun lun = new Lun();
                            for (DevicesData devicesData : counters.getCounters()) {
                                if ("Used Size".equals(devicesData.getName())) {
                                    used = used.add(this.unitConversion(devicesData.getValue(), devicesData.getUnit()));
                                }
                                if ("Total Size".equals(devicesData.getName())) {
                                    lun.setTotal(this.unitConversion(devicesData.getValue(), devicesData.getUnit()).toPlainString());
                                }
                                if ("Disk Utilization".equals(devicesData.getName()) && CommonUtils.isBigDecimal(devicesData.getValue())) {


                                    if(new BigDecimal(devicesData.getValue()).compareTo(new BigDecimal(90))>=0){
                                        warningDisk++;
                                        lun.setStatus("异常");
                                    }else{
                                        normalDisk++;
                                        lun.setStatus("正常");
                                    }

                                }
                            }

                            lun.setName(counters.getName());
                            lunList.add(lun);
                    }
                }

            }

            resultData.setUsedSize(this.showUnit(used));
            resultData.setNormalPower(normalPower);
            resultData.setWarningPower(warningPower);
            resultData.setNormalFan(normalFan);
            resultData.setWarningFan(warningFan);
            resultData.setNormalDisk(normalDisk);
            resultData.setWarningDisk(warningDisk);
            resultData.setLunList(lunList);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultData;
    }

//    //全按照MB来
//    private BigDecimal unitConversion(String size,String unit){
//        if (StringUtil.isEmpty(size) || size.equals("0")){
//            return new BigDecimal(0);
//        }
//
//        if ("GB".equals(unit)){
//            return new BigDecimal(size).multiply(new BigDecimal(1024));
//        }
//        if ("TB".equals(unit)){
//            return new BigDecimal(size).multiply(new BigDecimal(1024).multiply(new BigDecimal(1024)));
//        }
//        if ("KB".equals(unit)){
//            return new BigDecimal(size).divide(new BigDecimal(1024),BigDecimal.ROUND_CEILING);
//        }
//        return new BigDecimal(size);
//    }

    //全按照TB来
    private BigDecimal unitConversion(String size, String unit) {

        try {

            //处理数值过大问题
            size = this.dealE(size);

            if (StringUtil.isEmpty(size) || size.equals("0") || !CommonUtils.isBigDecimal(size)) {
                return new BigDecimal(0);
            }

            if ("GB".equals(unit)) {
                return new BigDecimal(size).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).setScale(2, BigDecimal.ROUND_CEILING);
            }
            if ("MB".equals(unit)) {
                return new BigDecimal(size).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).setScale(2, BigDecimal.ROUND_CEILING);
            }
            if ("KB".equals(unit)) {
                return new BigDecimal(size).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).setScale(2, BigDecimal.ROUND_CEILING);
            }
            if ("B".equals(unit)) {
                return new BigDecimal(size).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).setScale(2, BigDecimal.ROUND_CEILING);
            }
            return new BigDecimal(size).setScale(2, BigDecimal.ROUND_CEILING);

        }catch (Exception e){
            e.printStackTrace();
            return new BigDecimal(0);
        }
    }

    public String dealE(String size) {
        if (!size.contains("E")) {
            return size;
        }
        String[] sizes = size.split("E");

        return new BigDecimal(sizes[0]).multiply(new BigDecimal(10).pow(Integer.parseInt(sizes[1]))).toString();
    }

    //返回值按TB来
    private BigDecimal showUnit(BigDecimal size) {
//        if (size.compareTo(new BigDecimal(0)) > 0) {
//            return size.divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).divide(new BigDecimal(1024), BigDecimal.ROUND_CEILING).setScale(2, RoundingMode.HALF_UP);
//        }
        return size;
    }

    public String getType(String fdn) {

        try {

            String url = env.getProperty("gatewaySystem.url");
            String header = env.getProperty("gatewaySystem.header");
            String queryUrl = url + deviceType + "?device_fdns=" + fdn;
            String json = "";
            if (!"prod".equals(env.getProperty("spring.profiles.active"))) {
                File jsonFile = ResourceUtils.getFile(monitorTypePath);
                json = CommonUtils.getStr(jsonFile);

            } else {
                HttpResult res = NoSslHttpUtils.noSslGet(queryUrl, header);
                json = res.getBody();
            }
            List<MonitorsSummary> monitorsList = JSONArray.parseArray(json, MonitorsSummary.class);
            MonitorsSummary monitorsSummary = monitorsList.get(0);

            for (Monitors monitors : monitorsSummary.getMonitors()) {

                if ("HuaweiStorageDisk".equals(monitors.getType())) {
                    return "huawei";
                }

                if ("EMCIsilonNodeCPU".equals(monitors.getType())) {
                    return "dell";
                }

                if ("NetAppsCPUMonitor".equals(monitors.getType())) {
                    return "netApp";
                }

                if ("SugonStorageCPU".equals(monitors.getType())) {
                    return "sugon";
                }

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //取华为设备的控制器和磁盘
    public BigDecimal syncHuaweiItem(String fdn, String ip) {
        String json = this.getDeviceMonitors(fdn);
        List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
        DevicesMonitors monitors = monitorsList.get(0);
        BigDecimal total = new BigDecimal(0);
        List<RealStorageItem> itemList = new ArrayList<>();
        RealStorage storage = new RealStorage();
        BigDecimal used = new BigDecimal(0);

        for (DevicesCounters counters : monitors.getMonitors()) {

            //磁盘
            if ("HuaweiStorageDisk".equals(counters.getType())) {
                RealStorageItem distItem = new RealStorageItem();
                //磁盘监控
                if (counters.getCounters() == null || counters.getCounters().size() == 0) {
                    distItem.setStatus("2");
                } else {
                    for (DevicesData devicesData : counters.getCounters()) {

                        if ("TotalCapacity".equals(devicesData.getName())) {
                            total = total.add(this.unitConversion(devicesData.getValue(), devicesData.getUnit()));
                        }

                        if ("HealthStatus".equals(devicesData.getName())) {
                            distItem.setStatus(devicesData.getValue());
                        }

                    }
                }
                distItem.setName(counters.getName());
                distItem.setFdn(fdn);
                distItem.setType("disk");
                if (counters.getName().contains(".")) {
                    String[] names = counters.getName().split("\\.");
                    distItem.setKeyword(names[0]);
                }
                itemList.add(distItem);
            }

            //风扇
            if ("HuaweiStorageFan".equals(counters.getType())) {
                RealStorageItem fanItem = new RealStorageItem();

                if (counters.getCounters() == null || counters.getCounters().size() == 0) {
                    fanItem.setStatus("2");
                } else {
                    for (DevicesData devicesData : counters.getCounters()) {
                        if ("HealthStatus".equals(devicesData.getName())) {
                            fanItem.setStatus(devicesData.getValue());
                        }
                    }
                }
                fanItem.setName(counters.getName());
                fanItem.setType("fan");
                fanItem.setKeyword("fan");
                fanItem.setFdn(fdn);
                itemList.add(fanItem);
            }

            //电源
            if ("HuaweiStoragePower".equals(counters.getType())) {
                RealStorageItem fanItem = new RealStorageItem();

                if (counters.getCounters() == null || counters.getCounters().size() == 0) {
                    fanItem.setStatus("2");
                } else {
                    for (DevicesData devicesData : counters.getCounters()) {
                        if ("HealthStatus".equals(devicesData.getName())) {
                            fanItem.setStatus(devicesData.getValue());
                        }
                    }
                }
                fanItem.setName(counters.getName());
                fanItem.setType("power");
                fanItem.setKeyword("power");
                fanItem.setFdn(fdn);
                itemList.add(fanItem);
            }

            //控制器
            if ("HuaweiStorageController".equals(counters.getType())) {
                RealStorageItem fanItem = new RealStorageItem();

                if (counters.getCounters() == null || counters.getCounters().size() == 0) {
                    fanItem.setStatus("2");
                } else {
                    for (DevicesData devicesData : counters.getCounters()) {
                        if ("HealthStatus".equals(devicesData.getName())) {
                            fanItem.setStatus(devicesData.getValue());
                        }
                    }
                }
                fanItem.setName(counters.getName());
                fanItem.setType("controller");
                fanItem.setKeyword("controller");
                fanItem.setFdn(fdn);
                itemList.add(fanItem);
            }

            if ("PingMonitor".equals(counters.getType())) {
                if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                    for (DevicesData devicesData : counters.getCounters()) {
                        if ("Response Time".equals(devicesData.getName())) {
                            storage.setPingTime(devicesData.getValue());
                        }
                    }
                }
            }

            if ("HuaweiStorageLUN".equals(counters.getType())) {
                //磁盘监控
                for (DevicesData devicesData : counters.getCounters()) {

                    if ("TotalCapacity".equals(devicesData.getName())) {
                        used = used.add(this.unitConversion(devicesData.getValue(), devicesData.getUnit()));
                    }

                }
            }


        }

        for (RealStorageItem item : itemList) {
            item.setBrand("huawei");
            item.setIp(ip);
            item.setId(UUID.randomUUID().toString().replace("-", ""));
        }

        storage.setId(UUID.randomUUID().toString().replace("-", ""));
        storage.setTotalCapacity(total);
        storage.setFdn(fdn);
        storage.setUsed(used);
        storageMapper.deleteByFdn(fdn);
        storageMapper.insert(storage);

        if (itemList.size() > 0) {
            storageItemMapper.deleteByFdn(fdn);
            storageItemMapper.insertBatch(itemList);
        }
        return used;
    }

    public BigDecimal syncSugonItem(String fdn, String ip) {

        List<VirtuChildResult> childResultList = this.getChildMOs(fdn);
        //拿ping时间
        String pingTime = this.getPingTime(fdn);
        for (VirtuChildResult r : childResultList) {

            if (isIp(r.getLabel())) {
                this.saveSugonItem(r.getFdn(), fdn, r.getLabel(), pingTime);
            }

        }

        BigDecimal used = lunMapper.selectUsedByFdn(fdn);
        if (used!=null){
            used = used.multiply(new BigDecimal(0.66));
        }
        return used;
    }

    public String getPingTime(String fdn) {
        String json = this.getDeviceMonitors(fdn);
        String pingTime = "";
        List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
        DevicesMonitors monitors = monitorsList.get(0);
        for (DevicesCounters counters : monitors.getMonitors()) {
            if ("PingMonitor".equals(counters.getType())) {
                for (DevicesData devicesData : counters.getCounters()) {
                    if ("Response Time".equals(devicesData.getName())) {
                        return devicesData.getValue();
                    }
                }
            }
        }
        return pingTime;
    }

    public void saveSugonItem(String sonFdn, String saveFdn, String label, String pingTime) {
        String json = this.getDeviceMonitors(sonFdn);

        List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
        DevicesMonitors monitors = monitorsList.get(0);
        List<RealStorageItem> itemList = new ArrayList<>();
        RealStorage storage = new RealStorage();
        RealStorageLun lun = new RealStorageLun();


        for (DevicesCounters counters : monitors.getMonitors()) {
            //磁盘
            if ("SugonStorageNodeDiskHealth".equals(counters.getType())) {
                RealStorageItem distItem = new RealStorageItem();
                //磁盘监控
                if (counters.getCounters() == null || counters.getCounters().size() == 0) {
                    distItem.setStatus("2");
                } else {
                    for (DevicesData devicesData : counters.getCounters()) {

                        if ("BadSectors".equals(devicesData.getName())) {
                            distItem.setStatus("0".equals(devicesData.getValue()) ? "1" : "2");
                        }

                    }
                }
                distItem.setName(counters.getName());
                distItem.setFdn(saveFdn);
                distItem.setType("disk");
                distItem.setIp(label);
                distItem.setKeyword(label);

                itemList.add(distItem);
            }

            //控制器
            if ("SugonStorageNodeDiskController".equals(counters.getType())) {
                RealStorageItem distItem = new RealStorageItem();
                //磁盘监控
                if (counters.getCounters() == null || counters.getCounters().size() == 0) {
                    distItem.setStatus("2");
                } else {
                    for (DevicesData devicesData : counters.getCounters()) {

                        if ("State".equals(devicesData.getName())) {
                            distItem.setStatus("ok".equals(devicesData.getValue()) ? "1" : "2");
                        }

                    }
                }
                distItem.setName(counters.getName());
                distItem.setFdn(saveFdn);
                distItem.setType("controller");
                distItem.setIp(label);
                distItem.setKeyword(label);
                itemList.add(distItem);
            }

            if ("SugonStorageNodeDiskGlobalUsage".equals(counters.getType())) {
                //磁盘监控
                if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                    for (DevicesData devicesData : counters.getCounters()) {
                        //磁盘总
                        if ("Max".equals(devicesData.getName())) {
                            storage.setTotalCapacity(this.unitConversion(devicesData.getValue(), devicesData.getUnit()));
                        }

                        //已用
                        if ("Used".equals(devicesData.getName())) {
                            lun.setUsed(this.unitConversion(devicesData.getValue(), devicesData.getUnit()));
                        }
                        //未使用
                        if ("Unused".equals(devicesData.getName())) {
                            BigDecimal total = this.unitConversion(devicesData.getValue(), devicesData.getUnit()).add(lun.getUsed());
                            lun.setTotal(total);
                        }



                    }
                }
            }

            if ("SugonStorageNodeStatus".equals(counters.getType())) {
                //磁盘监控
                if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                    for (DevicesData devicesData : counters.getCounters()) {
                        //状态
                        if ("Status".equals(devicesData.getName())) {
                            lun.setStatus("NODE_STATE_HEALTHY".equals(devicesData.getValue()) ? "正常" : "异常");
                        }

                    }
                }
            }


            for (RealStorageItem item : itemList) {
                item.setBrand("sugon");
                item.setId(UUID.randomUUID().toString().replace("-", ""));
            }

            if (!"0.0.0.0".equals(label)){
                lun.setFdn(saveFdn);
                lun.setId(UUID.randomUUID().toString().replace("-", ""));
                lun.setName(label);
                lunMapper.deleteByFdnAndName(saveFdn, lun.getName());
                lunMapper.insert(lun);
            }

            storage.setId(UUID.randomUUID().toString().replace("-", ""));
            storage.setFdn(saveFdn);
            storage.setPingTime(pingTime);
            storageMapper.deleteByFdn(saveFdn);
            storageMapper.insert(storage);

            if (itemList.size() > 0) {
                storageItemMapper.deleteByIp(label);
                storageItemMapper.insertBatch(itemList);
            }


        }
    }


    public BigDecimal syncDellUsed(String fdn) {

        try {

            String json = this.getDeviceMonitors(fdn);
            List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
            DevicesMonitors monitors = monitorsList.get(0);

            for (DevicesCounters counters : monitors.getMonitors()) {


                if ("EMCIsilonClusterIFSUtil".equals(counters.getType())) {
                    if (counters.getCounters() != null && counters.getCounters().size() > 0) {
                        for (DevicesData devicesData : counters.getCounters()) {
                            if ("Used".equals(devicesData.getName())) {
                                return this.unitConversion(devicesData.getValue(),devicesData.getUnit());
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BigDecimal(0);
    }



    public boolean isIp(String label) {
        String[] labels = label.split("\\.");
        if (labels.length == 4) {
            return true;
        }
        return false;
    }

}
